<template>
  <div>
    <el-dialog :visible.sync="visible" v-dialogDrag title="机票申请" :close-on-click-modal="false" class="tl-dialog" width="1257px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
      <iframe :src="toaUrl" frameborder="0" width="100%" height="500px" scrolling="auto"></iframe>
    </el-dialog>
    <el-dialog :visible.sync="attachmentModel.visible" v-dialogDrag :title="attachmentModel.title" :close-on-click-modal="false" class="tl-dialog" width="95%" :append-to-body="true" top="5px" :modal-append-to-body="true">
      <iframe :src="attachmentModel.url" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@tapui/utils/auth'
export default {
  data() {
    return {
      toaUrl: '',
      url: {
        saveOrder: '/base/systemRelation/saveData'
      },
      model: {},
      attachmentModel: {
        url: '',
        title: '',
        visible: false
      },
      isAdd: false,
      visible: false,
      title: 'OA机票申请单',
      id: 0
    }
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    },
    sourceId: {
      type: Number,
      default: 0
    },
    relationSourceType: {
      type: String,
      default: ''
    }
  },
  methods: {
    open() {
      // 为了兼容2.0的附件预览功能，top.openBigDialog
      window.openBigDialog = (obj) => {
        this.attachmentModel.title = obj.title
        this.attachmentModel.url = `/toa/cbo/cboAttachment_componentDispIndex.action?${obj.url.split('?')[1]}`
        this.attachmentModel.visible = true
      }

      this.model = {
        sourcetype: this.sourceType,
        sourceid: this.sourceId,
        relationsystem: 'Toa',
        relationsourcetype: this.relationSourceType,
        title: this.title
      }
      let url = '/toa/toa/toaPassengerTicket_load.action'
      url = `${url}?id=${this.id}&authtoken=${getToken()}`
      if (this.isAdd) {
        url = `${url}&uiOperateState=1`
      } else {
        url = `${url}&uiOperateState=2`
      }
      this.toaUrl = url
      let that = this
      addEventListener('message', that.saveOrder)
    },
    saveOrder(info) {
      if (info.data.indexOf(':') > -1) {
        this.$set(this.model, 'relationsourceid', info.data.split(':')[1])
        this.$tap.http.postAction(this.url.saveOrder, this.model).then((res) => {
          if (res.success) {
            this.$emit('refresh')
          } else {
            this.$tap.alert(res.message)
          }
        })
      }
    },
    close() {
      let that = this
      removeEventListener('message', that.saveOrder)
    }
  }
}
</script>

<style></style>
